<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button id="btn1">2秒后弹出你好</button>
    <button id="btn2">取消弹出</button>

    <script>
        var oBtn1 = document.getElementById('btn1');
        var oBtn2 = document.getElementById('btn2');

        // 设置一个全局变量
        var timer;

        oBtn1.onclick = function () {
            timer = setTimeout(function () {
                alert('你好');
            }, 2000);
        }

        oBtn2.onclick = function () {
            clearTimeout(timer);
        }
    </script>
</body>

</html>